import api from '@/api';
import { array } from 'prop-types';

export default {
  namespace: 'app',

  state: {
    appId: {},
    app: [],
    protocols: [],
    type: {},
    domain: {},
    backAppList: {
      list: [],
      pagination: {},
    },
    bindList: {
      list: [],
      pagination: {},
    },
    appConfigList: {
      list: [],
      pagination: {},
    },
    internalAppUserList: {
      list: [],
      pagination: {},
    },
    airApp: [],
  },

  effects: {
    *createFrontApp({ payload }, { call, put }) {
      return yield call(api.app.user.create, payload);
    },
    *updateFrontApp({ payload }, { call, put }) {
      return yield call(api.app.user.update, payload);
    },
    *lockFrontApp({ payload }, { call, put }) {
      return yield call(api.app.user.lock, payload);
    },
    *unlockFrontApp({ payload }, { call, put }) {
      return yield call(api.app.user.unlock, payload);
    },
    *resetFrontAppSecret({ payload }, { call, put }) {
      return yield call(api.app.user.resetSecret, payload);
    },
    *deleteFrontApp({ payload }, { call, put }) {
      return yield call(api.app.user.delete, payload);
    },
    *batchDeleteFrontApp({ payload }, { call, put }) {
      return yield call(api.app.user.batchDelete, payload);
    },
    *queryFrontAppDetail({ payload }, { call, put }) {
      return yield call(api.app.user.detail, payload);
    },
    *queryFrontAppList({ payload }, { call, put }) {
      return yield call(api.app.user.list, payload);
    },

    *createBackApp({ payload }, { call, put }) {
      return yield call(api.app.system.create, payload);
    },
    *updateBackApp({ payload }, { call, put }) {
      return yield call(api.app.system.update, payload);
    },
    *deleteBackApp({ payload }, { call, put }) {
      return yield call(api.app.system.batchDelete, payload);
    },
    *queryAppDetail({ payload }, { call, put }) {
      return yield call(api.app.system.detail, payload);
    },
    *queryBackAppList({ payload }, { call, put }) {
      const res = yield call(api.app.system.list, payload);
      if (res.errorCode !== 0) {
        return;
      }
      yield put({
        type: 'saveBackAppList',
        payload: {
          list: res.data,
          pagination: res.pagination,
        },
      });
    },

    *bindUser({ payload }, { call, put }) {
      return yield call(api.app.bind.bind, payload);
    },
    *unbindUser({ payload }, { call, put }) {
      return yield call(api.app.bind.unbind, payload);
    },
    *resetUserSecret({ payload }, { call, put }) {
      return yield call(api.app.bind.resetSecret, payload);
    },
    *lockUser({ payload }, { call, put }) {
      return yield call(api.app.bind.lock, payload);
    },
    *unlockUser({ payload }, { call, put }) {
      return yield call(api.app.bind.unlock, payload);
    },
    *resetUserExpiredate({ payload }, { call, put }) {
      return yield call(api.app.bind.resetExpiredate, payload);
    },
    *queryBindList({ payload }, { call, put }) {
      const res = yield call(api.app.bind.list, payload);
      if (res.errorCode !== 0) {
        return;
      }
      yield put({
        type: 'saveBindList',
        payload: {
          list: res.data,
          pagination: res.pagination,
        },
      });
    },

    *createAppConfig({ payload }, { call, put }) {
      return yield call(api.app.appConfig.create, payload);
    },
    *updateAppConfig({ payload }, { call, put }) {
      return yield call(api.app.appConfig.update, payload);
    },
    *unbindAppConfig({ payload }, { call, put }) {
      return yield call(api.app.appConfig.unbind, payload);
    },
    *queryAppConfigList({ payload }, { call, put }) {
      const res = yield call(api.app.appConfig.list, payload);
      if (res.errorCode !== 0) {
        return;
      }
      yield put({
        type: 'saveAppConfigList',
        payload: {
          list: res.data,
          pagination: res.pagination,
        },
      });
    },
    *queryAppConfigDetail({ payload }, { call, put }) {
      return yield call(api.app.appConfig.detail, payload);
    },

    *createInternalApp({ payload }, { call, put }) {
      return yield call(api.app.internal.create, payload);
    },
    *updateInternalApp({ payload }, { call, put }) {
      return yield call(api.app.internal.update, payload);
    },
    *deleteInternalApp({ payload }, { call, put }) {
      return yield call(api.app.internal.delete, payload);
    },
    *queryInternalAppList({ payload }, { call, put }) {
      return yield call(api.app.internal.list, { payload });
    },

    *bindInternalAppUser({ payload }, { call, put }) {
      return yield call(api.app.internalAppUser.bind, payload);
    },
    *unbindInternalAppUser({ payload }, { call, put }) {
      return yield call(api.app.internalAppUser.unbind, payload);
    },
    *queryInternalAppUserList({ payload }, { call, put }) {
      const res = yield call(api.app.internalAppUser.list, payload);
      if (res.errorCode !== 0) {
        return;
      }
      yield put({
        type: 'saveInternalAppUserList',
        payload: {
          list: res.data,
          pagination: res.pagination,
        },
      });
    },

    *queryDomain({ payload }, { call, put }) {
      const res = yield call(api.app.internal.domain, payload);
      if (res.errorCode !== 0) {
        return;
      }
      yield put({
        type: 'saveDomain',
        payload: res.data,
      });
    },
    *queryProtocols(_, { call, put }) {
      const res = yield call(api.app.protocols);
      if (res.errorCode !== 0) {
        return;
      }
      yield put({
        type: 'saveProtocols',
        payload: res.data,
      });
    },
    *queryTypes(_, { call, put }) {
      const res = yield call(api.app.type);
      if (res.errorCode !== 0) {
        return;
      }
      yield put({
        type: 'saveTypes',
        payload: res.data,
      });
    },
    *queryApp(_, { call, put }) {
      const res = yield call(api.app.base);
      if (res.errorCode !== 0) {
        return;
      }
      yield put({
        type: 'saveApp',
        payload: res.data,
      });
    },
    *queryAppId(_, { call, put }) {
      return yield call(api.sysSet.appId);
    },
    *resetSecret(_, { call, put }) {
      return yield call(api.sysSet.resetSecret);
    },
    *updateAgent({ payload }, { call, put }) {
      return yield call(api.sysSet.updateAgentUser, payload);
    },
    *queryAirApp({ payload }, { call, put }) {
      const res = yield call(api.app.system.airApp, payload);
      if (res.errorCode !== 0) {
        return;
      }
      yield put({
        type: 'saveAirApp',
        payload: res.data,
      });
    },
  },

  reducers: {
    saveInternalAppUserList(state, { payload }) {
      return {
        ...state,
        internalAppUserList: payload,
      };
    },
    saveBindList(state, { payload }) {
      return {
        ...state,
        bindList: payload,
      };
    },
    saveAppConfigList(state, { payload }) {
      return {
        ...state,
        appConfigList: payload,
      };
    },
    saveBackAppList(state, { payload }) {
      return {
        ...state,
        backAppList: payload,
      };
    },
    saveDomain(state, { payload }) {
      return {
        ...state,
        domain: payload,
      };
    },
    saveProtocols(state, { payload }) {
      return {
        ...state,
        protocols: payload,
      };
    },
    saveTypes(state, { payload }) {
      return {
        ...state,
        type: payload,
      };
    },
    saveApp(state, { payload }) {
      return {
        ...state,
        app: payload,
      };
    },
    saveAirApp(state, { payload }) {
      return {
        ...state,
        airApp: payload,
      };
    },
  },
};
